/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.core.util.mail;

import javax.mail.internet.MimeMessage;
import org.apache.wicket.injection.Injector;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;

/**
 * E-mail message preparator
 *
 * @author molnar.alexander@sonrisa.hu
 */
public class MimeMessagePreparatorImpl implements MimeMessagePreparator
{

  // --------------------------------------------------
  // ~ Constants
  // --------------------------------------------------
  // --------------------------------------------------
  // ~ Services
  // --------------------------------------------------
  // --------------------------------------------------
  // ~ Members
  // --------------------------------------------------
  private String encoding;
  private String fromEmailAddress;
  private String targetEmailAddress;
  private String subject;
  private String text;

  // --------------------------------------------------
  // ~ Constructors
  // --------------------------------------------------
  public MimeMessagePreparatorImpl()
  {
    Injector.get().inject(this);
  }

  // --------------------------------------------------
  // ~ Overrided methods
  // --------------------------------------------------
  // --------------------------------------------------
  // ~ Private methods
  // --------------------------------------------------
  // --------------------------------------------------
  // ~ Protected methods
  // --------------------------------------------------
  // --------------------------------------------------
  // ~ Public methods
  // --------------------------------------------------
  public void prepare(final MimeMessage mimeMessage) throws Exception
  {
    final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, encoding);

    message.setTo(targetEmailAddress);
    message.setFrom(fromEmailAddress);
    message.setSubject(subject);
    message.setText(text, true);
  }

  // --------------------------------------------------
  // ~ Getters and Setters
  // --------------------------------------------------
  public void setEncoding(final String encoding)
  {
    this.encoding = encoding;
  }

  public void setFromEmailAddress(final String fromEmailAddress)
  {
    this.fromEmailAddress = fromEmailAddress;
  }

  public void setTargetEmailAddress(final String targetEmailAddress)
  {
    this.targetEmailAddress = targetEmailAddress;
  }

  public void setSubject(final String subject)
  {
    this.subject = subject;
  }

  public void setText(final String text)
  {
    this.text = text;
  }
  // --------------------------------------------------
  // ~ Inner Class
  // --------------------------------------------------
}
